package edu.wuster.blog.controller;

import edu.wuster.blog.entity.Blogpart;
import edu.wuster.blog.entity.Result;
import edu.wuster.blog.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.List;



@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    //管理员搜索
    @GetMapping("/admin")
    public Result selectByAdmin(String title, String state, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("管理员查询");
        List<Blogpart> p = searchService.selectByAdmin(title, state, begin, end);
        log.info("管理员查询成功");
        return Result.success(p);
    }

    @GetMapping("/selectByAuthor/{author}")
    public Result selectByAuthor(@PathVariable String author) {
        log.info("根据用户名查看所有博客");
        List<Blogpart> list = searchService.selectByAuthor(author);
        log.info("查看成功");
        return Result.success(list);
    }

    @GetMapping("/selectByLabel/{name}")
    public Result selectByLabel(@PathVariable String name) {
        log.info("根据标签查找博客");
        List<Blogpart> list = searchService.selectByLabel(name);
        log.info("查看成功");
        return Result.success(list);
    }

    @GetMapping("/userSelectMohu")
    public Result userSelectMohu(@RequestParam String mohu, @RequestParam String author) {
        log.info("用户模糊查找本人的博客");
        List<Blogpart> list = searchService.userSelectMohu(mohu, author);
        return Result.success(list);
    }

    @GetMapping("/homeSelectMohu")
    public Result homeSelectMohu(String title, Integer likeNum, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("首页模糊查找博客");
        List<Blogpart> p = searchService.homeSelectMohu(title,likeNum,begin,end);
        log.info("查找成功");
        return Result.success(p);
    }

}
